﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Convertor.Control
{
    /// <summary>
    /// Loading.xaml 的交互逻辑
    /// </summary>
    public partial class Loading : UserControl
    {
        Storyboard sb;
        public Loading()
        {
            InitializeComponent();
            this.Visibility = System.Windows.Visibility.Collapsed;

            sb = new Storyboard();
            DoubleAnimation rotate = new DoubleAnimation();
            rotate.From = 0;
            rotate.To = 360;
            rotate.RepeatBehavior = RepeatBehavior.Forever;

            RotateTransform rt = new RotateTransform();
            image1.RenderTransformOrigin = new Point(0.5, 0.5);
            image1.RenderTransform = rt;
            Storyboard.SetTarget(rotate, image1);
            Storyboard.SetTargetName(rotate, image1.Name);
            Storyboard.SetTargetProperty(rotate, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
            sb.Children.Add(rotate);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //BackgroundWorker asd = new BackgroundWorker();
            //asd.DoWork += new DoWorkEventHandler(asd_DoWork);
            //asd.RunWorkerCompleted += new RunWorkerCompletedEventHandler(asd_RunWorkerCompleted);
            //asd.RunWorkerAsync();
        }

        public void Start()
        {
            this.Dispatcher.Invoke(() => { this.Visibility = System.Windows.Visibility.Visible; });
            sb.Begin(image1, true);
        }

        public void Stop()
        {
            if (sb != null)
            {
                sb.Stop(image1);
            }
            this.Dispatcher.Invoke(() => { this.Visibility = System.Windows.Visibility.Collapsed; });
        }
    }
}
